
---
title: "Regional analysis"
author: "Francesco Bailo"
date: "12/14/2016"
output: pdf_document
---

```{r, echo = FALSE, message = FALSE, warning = FALSE}
setwd("~/Documents/replication_packages/eps_2016/")
df <- read.csv('regional_voting_data.csv')

require(reshape2)

## Add Gini index
regional_gini_index <- 
  read.csv("istat_regional_gini_index.csv")
regional_gini_index <- subset(regional_gini_index,
                              Including.or.not.including.imputed.rents == "including imputed rents",
                              select = c("istat_cod_reg", "Year", "X0"))
names(regional_gini_index)[3] <- "gini_index"                             
df <- merge(df, regional_gini_index[], 
            by.x = c("regione_cod_istat","anno"), 
            by.y = c("istat_cod_reg","Year"), all.x = TRUE)


# PIL in '000
df$pil_pro_capite <- df$pil_pro_capite / 1000

# Reduce to decimals
df$perc_lega <- df$perc_lega/100
df$perc_pop_straniera <- df$perc_pop_straniera/100
df$affluenza <- df$affluenza/100
df$perc_disoc <- df$perc_disoc/100
df$perc_rapine <- df$perc_rapine/100000
df$perc_furti <- df$perc_furti/100000
df$perc_omicidi <- df$perc_omicidi/100000
df$perc_crimine_percepito <- df$perc_crimine_percepito/100

require(plm)
fe.mod0 <- plm(perc_lega ~ perc_pop_straniera,
               index = c("anno","regione"), model = "within", data = df)
fe.mod1 <- plm(perc_lega ~ perc_pop_straniera + affluenza,
               index = c("anno","regione"), model = "within", data = df)
fe.mod2 <- plm(perc_lega ~ perc_pop_straniera + affluenza  +  lega_govern_region,
               index = c("anno","regione"), model = "within", data = df)
fe.mod3 <- plm(perc_lega ~ perc_pop_straniera + affluenza + lega_govern_region + 
                 pil_pro_capite + perc_disoc, 
               index = c("anno","regione"), model = "within", data = df)
fe.mod4 <- plm(perc_lega ~ perc_pop_straniera + affluenza + lega_govern_region + 
                 pil_pro_capite +  perc_disoc +
                 perc_furti,
               index = c("anno","regione"), model = "within", data = df)
fe.mod5 <- plm(perc_lega ~ perc_pop_straniera  + affluenza +
                 pil_pro_capite + lega_govern_region +
                 perc_furti + perc_omicidi,
               index = c("anno","regione"), model = "within", data = df)
fe.mod6 <- plm(perc_lega ~ perc_pop_straniera + affluenza +
                 pil_pro_capite + lega_govern_region +
                 perc_furti + perc_omicidi + perc_rapine,
               index = c("anno","regione"), model = "within", data = df)
fe.mod7 <- plm(perc_lega ~ perc_pop_straniera + 
                 affluenza + lega_govern_region +
                 pil_pro_capite + lega_govern_region +
                 perc_furti + perc_omicidi + perc_rapine +
                 perc_crimine_percepito,
               index = c("anno","regione"), model = "within", data = df)

fe.mod8 <- plm(perc_lega ~ perc_pop_straniera + 
                 affluenza + lega_govern_region +
                 pil_pro_capite + lega_govern_region +
                 perc_furti + perc_omicidi + perc_rapine +
                 perc_crimine_percepito + perc_pop_straniera * perc_crimine_percepito,
               index = c("anno","regione"), model = "within", data = df)

## Test
pool.mod7 <- plm(perc_lega ~ perc_pop_straniera + 
                   affluenza + lega_govern_region +
                   pil_pro_capite + lega_govern_region +
                   perc_furti + perc_omicidi + perc_rapine +
                   perc_crimine_percepito,
                 index = c("anno","regione"), model = "pooling", data = df)
# pFtest(fe.mod7, pool.mod7)

df_complete <- df[,c('perc_lega', 'perc_pop_straniera', 'affluenza', 
                     'lega_govern_region', 'pil_pro_capite', 
                     'lega_govern_region', 'perc_furti', 'perc_omicidi',
                     'perc_rapine', 'perc_crimine_percepito', 
                     'anno', 'regione')]
df_complete <- df_complete[complete.cases(df_complete),]
require(pcse)
lm.mod7 <- lm(perc_lega ~ perc_pop_straniera + 
                affluenza + lega_govern_region +
                pil_pro_capite + lega_govern_region +
                perc_furti + perc_omicidi + perc_rapine +
                perc_crimine_percepito, data = df_complete)
pcse.mod7 <- pcse(lm.mod7, groupN = df_complete$regione, groupT = df_complete$anno)

# summary(plm(perc_crimine_percepito ~ perc_furti + perc_rapine + perc_omicidi, 
#             index = c("anno","regione"), model = 'pool',
#             data = df))
# summary(plm(perc_crimine_percepito ~ perc_furti + perc_rapine + perc_omicidi, 
#             index = c("anno","regione"), model = 'within',
#             data = df))

# Crime immigration
require(car)

crime_fe.mod1 <- 
  plm(perc_crimine_percepito ~ perc_pop_straniera + pil_pro_capite, data = df,
      index = c("anno","regione"), model = 'within')
crime_fe.mod2 <- 
  plm(perc_furti ~ perc_pop_straniera + pil_pro_capite, data = df,
      index = c("anno","regione"), model = 'within')
crime_fe.mod3 <- 
  plm(perc_rapine ~ perc_pop_straniera, data = df,
      index = c("anno","regione"), model = 'within')
crime_fe.mod4 <- 
  plm(perc_omicidi ~ perc_pop_straniera, data = df,
      index = c("anno","regione"), model = 'within')

scatterplotMatrix( ~ perc_lega + perc_pop_straniera + perc_crimine_percepito + perc_furti + 
                     perc_rapine + perc_omicidi,
                   var.labels = c("votes","foreignPop","crimePerception",
                                  "thefts","robberies","homicides"),
                   data = df)

```

```{r, echo = FALSE, results = 'asis', message = FALSE, warning = FALSE}
require(stargazer)
stargazer(fe.mod1, fe.mod2, fe.mod3, fe.mod4, fe.mod5, fe.mod6, fe.mod7, pool.mod7,
          dep.var.labels=c("votes"),
          covariate.labels=c("foreignPop","turnout","LegaGoverningRegion","procapiteGDP",
                             "unemp","thefts","homicides","robberies","crimePerception"),
          omit.stat = "f",
          single.row=FALSE, font.size = 'tiny')
```

```{r, echo = FALSE, message = FALSE, warning = FALSE}
require(ggplot2)

ggplot(df, aes(x=anno, y=perc_lega, group=regione, colour=regione)) +
  geom_point() + geom_line() + theme_bw() +
  theme(legend.position="bottom") +
  guides(colour = guide_legend(nrow=2, byrow=TRUE))
```


```{r, echo = FALSE, message = FALSE, warning = FALSE}
immigration_data <- read.csv('regional_immigration_data.csv')

require(dplyr)
require(zoo)
immigration_data_nospline <- 
  subset(immigration_data, anno > 2000) %>%
  dplyr::group_by(cod_istat_regione) %>%
  dplyr::arrange(cod_istat_regione, anno) %>%
  dplyr::mutate(year_diff = perc / lag(perc),
                year_mean_diff = rollapply(year_diff, 3, align = "right", fill = NA, mean))

df_post2001 <-
  merge(df, immigration_data_nospline, all = FALSE, 
        by.x = c("anno","regione_cod_istat"), by.y = c("anno","cod_istat_regione"))

df_post2001$year_mean_diff <- with(df_post2001, (year_mean_diff - 1) * 100)
df_post2001$year_diff <- with(df_post2001, (year_diff - 1) * 100)

require(plm)
fe.mod0_post2001 <- 
  plm(perc_lega ~ year_diff,
      index = c("anno","regione"), model = "within", data = df_post2001)

```